package com.api.syh56.shengyunhui.controller.admin;

import com.api.syh56.shengyunhui.common.*;
import com.api.syh56.shengyunhui.entity.Invoice;
import com.api.syh56.shengyunhui.entity.vo.WAGParamVO;
import com.api.syh56.shengyunhui.entity.vo.WAGParmVOListAndInvoice;
import com.api.syh56.shengyunhui.service.AdminInvoiceService;
import com.api.syh56.shengyunhui.service.InvoiceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 发票管理
 */
@RestController
@RequestMapping("admin/invoice")
public class AdminInvoiceController {

    @Autowired
    private AdminInvoiceService adminInvoiceService;


    @Autowired
    private InvoiceService invoiceService;
    /**
     * 获取发票
     */
    @GetMapping("get")
    public CommonResult<Object> get(){
        List<Invoice> invoices = adminInvoiceService.get();
        return CommonResult.success(invoices);
    }

    /**
     * 后台发票管理 根据条件筛选发票
     * @param pageSize
     * @param pageNum
     * @param startDate
     * @param endDate
     * @param invoiceStatus
     * @param invoiceNum
     * @return
     */
    @GetMapping (value = "selectPageByuid")
    public CommonResult<CommonPage<Invoice>>  selectPageByuid(@RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                              @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") String startDate,
                                                              @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") String endDate,
                                                              @RequestParam String invoiceStatus,
                                                              @RequestParam String invoiceNum
                                                              ) {
        QueryWrapper<Invoice> invoiceQueryWrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate)){
            // 解析传入的年月字符串
            LocalDateTime startDateTime = LocalDateTime.of(
                    Integer.parseInt(startDate.substring(0, 4)),
                    Integer.parseInt(startDate.substring(5, 7)),
                    Integer.parseInt(startDate.substring(8, 10))
                    , 0, 0);
            LocalDateTime endDateTime = LocalDateTime.of(
                    Integer.parseInt(endDate.substring(0, 4)),
                    Integer.parseInt(endDate.substring(5, 7)),
                    Integer.parseInt(endDate.substring(8, 10))
                    , 0, 0);

            invoiceQueryWrapper.lambda().between(Invoice::getCreationTime,startDateTime,endDateTime);
        }

        if (StringUtils.isNotBlank(invoiceStatus)){
            Short invoiceStatusShort = Short.valueOf(invoiceStatus);
            invoiceQueryWrapper.lambda().eq(Invoice::getInvoiceState, invoiceStatusShort);
        }

        if (StringUtils.isNotBlank(invoiceNum)){
            invoiceQueryWrapper.lambda().eq(Invoice::getInvoiceNum, invoiceNum);
        }

        invoiceQueryWrapper.lambda().orderByDesc(Invoice::getCreationTime);
        Page<Invoice> page = new Page<>(pageNum,pageSize);
        Page<Invoice> invoicePage = invoiceService.page(page,invoiceQueryWrapper);
        return CommonResult.success(CommonPage.restPage(invoicePage));
    }




    /**
     * 上传发票（图片url）
     * @param invoice 发票id/url
     * @return
     */
    @PostMapping("addInvoiceUrl")
    public CommonResult<Object> addInvoiceUrl(@RequestBody Invoice invoice){
        String s = BaseUserInfo.get(UserInfoEnu.id.getCode());
        invoice.setAdminUserId(Long.valueOf(s));
        invoiceService.insertUrl(invoice);
        return CommonResult.success(null);
    }

    /**
     * 发票申请详情页(发票相关所有订单详情)
     * @param invoice
     * @return
     */
    @PostMapping("getdetails")
    private CommonResult<WAGParmVOListAndInvoice> getDetails(@RequestBody Invoice invoice){
        Long invoiceId = invoice.getId();
        WAGParmVOListAndInvoice wagParmVOListAndInvoice = adminInvoiceService.getDetails(invoiceId);
        return CommonResult.success(wagParmVOListAndInvoice);
    }
}
